--#include "data\config\misc\MeridianConfig.lua" once	--配置
--#include "data\functions\Activity\Common\Consumes.lua" --消耗 通用
--#include "data\functions\Consume.lua" once
--#include "data\functions\Activity\OpenServer\OpenServerPKRank.lua" once  




--升级处理处理
MiscsImplement.MeridianHandle = function( sysarg, msgid, packet )
	local meridianLevel = DataPack.readWord(packet)
	MeridianHandleImpl(sysarg, meridianLevel)
end


function MeridianHandleImpl(sysarg, nMeridian)
	local config = MeridianCfg.MeridianConsumes[nMeridian]
	if not config then
		System.trace("Can not find MeridianConsumes config, Meridian="..nMeridian)
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.Meridian004,ttFlyTip)
		return
	end

	local meridianlevel = Actor.getIntProperty(sysarg, PROP_ACTOR_MERIDIAND_LEVEL)

	if meridianlevel + 1 ~= nMeridian then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.Meridian005,ttFlyTip)
		return
	end
	
	--条件检测
	local bCheck = CheckMeridianCondition(sysarg, config)
	if bCheck ~= true then
		return
	end
	
	local currentSoul = Actor.getIntProperty(sysarg,PROP_ACTOR_MERIDIAN_SOUL)
	--local currentLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, MeridianCfg.activeLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0055, MeridianCfg.activeLevel),ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, config.consumes, 1, LogIds.MeridianLogId, Lang.LogTips.Meridian ) then
		return
	end
	
	--经络
	Actor.SetActorMeridian(sysarg, nMeridian)

	OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Merdian, nMeridian, 0)
	
	local level =  math.floor((nMeridian - 1)/MeridianCfg.stepNum) + 1
	local step = nMeridian - (level - 1) * MeridianCfg.stepNum
	local msg = string.format(Lang.ScriptTips.Meridian003, level, step)
	Actor.sendTipmsg(sysarg, msg, ttFlyTip)
end

--经络条件检测
function CheckMeridianCondition(sysarg, config)

	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if config.reqLevel and not Actor.checkActorLevel(sysarg, config.reqLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.Meridian001, config.reqLevel), ttFlyTip)
		return false
	end	

	if not CheckConsumeCond( sysarg, config.consumes, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.Meridian002, ttFlyTip)
		return false
	end
	
	return true
end


MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cMeridianLevelUp, MiscsImplement.MeridianHandle)